import tkinter as tk
from tkinter import ttk

from action.CommonLoginAction import submit_form
from commonDaoApi.impl.AppInfoDao import AInfoDao
from commonDaoApi.impl.TtzUserDao import TTZUserDao
from pojo.BaseLogin import BLogin

global_phoneNumbers = ""

def on_combobox_selected(combobox_platform_var,infoMap, loginWindow,event=None):
    selected_value = combobox_platform_var.get()
    print(f"选择的平台为: {selected_value}")
    bLogin = BLogin()
    bLogin.appId = infoMap.get(selected_value)
    global_phoneNumbers = TTZUserDao().queryAllUser(bLogin)
    print(global_phoneNumbers)
    # 获取所有的手机号
    ########## 手机号信息
    phoneValues = global_phoneNumbers
    print(global_phoneNumbers)
    comboboxPhone_var = tk.StringVar()
    combobox = ttk.Combobox(loginWindow, values=phoneValues, width=20, textvariable=comboboxPhone_var)
    combobox.grid(row=2, column=1)
    name_label = tk.Label(loginWindow, text="手机号:")
    name_label.grid(row=2, column=0)

    ########## 密码信息
    password_entry = tk.Entry(loginWindow)
    password_entry.grid(row=3, column=1)
    password_label = tk.Label(loginWindow, text="密码")
    password_label.grid(row=3, column=0)

    userToken_label = tk.Label(loginWindow, text="userToken的值将显示在这里")
    userToken_label.grid(row=4, columnspan=1)
    # 创建提交按钮
    submit_button = tk.Button(loginWindow, text="账号登录",command= lambda: submit_form(loginWindow,comboboxPlatform_var,comboboxPhone_var,password_entry))
    submit_button.grid(row=7, columnspan=2)

    exit_button = tk.Button(loginWindow, text="退出")
    exit_button.grid(row=8, columnspan=4)


if __name__ == '__main__':
    loginWindow = tk.Tk()
        # 创建姓名标签和输入框
    loginWindow.geometry("400x150")
    # 手机号下拉框
    # dao = UserDao()
    dao = AInfoDao()
    infoMap = dao.queryAllAppInfo()
    platformValues = []
    for key in infoMap.keys():
        platformValues.append(key)
    ########## 平台信息
    # platformValues =["天天钻","石头村"]
    comboboxPlatform_var = tk.StringVar()
    comboboxPlatform = ttk.Combobox(loginWindow, values=platformValues, width=20, textvariable=comboboxPlatform_var)
    # comboboxPlatform.bind('<<ComboboxSelected>>', lambda event: on_combobox_selected(comboboxPlatform_var, event))
    comboboxPlatform.bind('<<ComboboxSelected>>',lambda event: on_combobox_selected(comboboxPlatform_var,infoMap, loginWindow,event))
    comboboxPlatform.grid(row=1,column=1)
    platnformName_label = tk.Label(loginWindow, text="请选择平台名称:")
    platnformName_label.grid(row=1, column=0)

    loginWindow.mainloop()